/*
 * Hibernate_BookView.java
 */
package hibernate_book;

import core.*;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.hibernate.*;

/**
 * The application's main frame.
 */
public class Hibernate_BookView extends FrameView {

    public Hibernate_BookView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);
        jButton2.setVisible(false);
        Delete.setEnabled(false);
        HibernateUtil.setup("create table human_table (id int,second_name varchar,name varchar, last_name varchar);");
        HibernateUtil.setup("create table location (id int,caption varchar,phone varchar,adress varchar,idx int,parent_id int);");

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Hibernate_BookApp.getApplication().getMainFrame();
            aboutBox = new Hibernate_BookAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Hibernate_BookApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        Add = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        Find = new javax.swing.JButton();
        Back = new javax.swing.JButton();
        Fwd = new javax.swing.JButton();
        Delete = new javax.swing.JButton();
        Edit = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hibernate_book.Hibernate_BookApp.class).getContext().getResourceMap(Hibernate_BookView.class);
        Add.setIcon(resourceMap.getIcon("Add.icon")); // NOI18N
        Add.setText(resourceMap.getString("Add.text")); // NOI18N
        Add.setToolTipText(resourceMap.getString("Add.toolTipText")); // NOI18N
        Add.setName("Add"); // NOI18N
        Add.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AddMouseClicked(evt);
            }
        });

        jTextField1.setBackground(resourceMap.getColor("jTextField1.background")); // NOI18N
        jTextField1.setFont(resourceMap.getFont("jTextField1.font")); // NOI18N
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        Find.setIcon(resourceMap.getIcon("Find.icon")); // NOI18N
        Find.setText(resourceMap.getString("Find.text")); // NOI18N
        Find.setToolTipText(resourceMap.getString("Find.toolTipText")); // NOI18N
        Find.setName("Find"); // NOI18N
        Find.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                FindMouseClicked(evt);
            }
        });

        Back.setIcon(resourceMap.getIcon("Back.icon")); // NOI18N
        Back.setText(resourceMap.getString("Back.text")); // NOI18N
        Back.setToolTipText(resourceMap.getString("Back.toolTipText")); // NOI18N
        Back.setEnabled(false);
        Back.setName("Back"); // NOI18N
        Back.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BackMouseClicked(evt);
            }
        });

        Fwd.setIcon(resourceMap.getIcon("Fwd.icon")); // NOI18N
        Fwd.setText(resourceMap.getString("Fwd.text")); // NOI18N
        Fwd.setToolTipText(resourceMap.getString("Fwd.toolTipText")); // NOI18N
        Fwd.setEnabled(false);
        Fwd.setName("Fwd"); // NOI18N
        Fwd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                FwdMouseClicked(evt);
            }
        });

        Delete.setIcon(resourceMap.getIcon("Delete.icon")); // NOI18N
        Delete.setText(resourceMap.getString("Delete.text")); // NOI18N
        Delete.setToolTipText(resourceMap.getString("Delete.toolTipText")); // NOI18N
        Delete.setName("Delete"); // NOI18N
        Delete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                DeleteMouseClicked(evt);
            }
        });

        Edit.setIcon(resourceMap.getIcon("Edit.icon")); // NOI18N
        Edit.setText(resourceMap.getString("Edit.text")); // NOI18N
        Edit.setToolTipText(resourceMap.getString("Edit.toolTipText")); // NOI18N
        Edit.setName("Edit"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextField2.setBackground(resourceMap.getColor("jTextField2.background")); // NOI18N
        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField3.setBackground(resourceMap.getColor("jTextField3.background")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jSpinner1.setFont(resourceMap.getFont("jSpinner1.font")); // NOI18N
        jSpinner1.setToolTipText(resourceMap.getString("jSpinner1.toolTipText")); // NOI18N
        jSpinner1.setEnabled(false);
        jSpinner1.setName("jSpinner1"); // NOI18N
        jSpinner1.setValue(0);
        jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner1StateChanged(evt);
            }
        });

        jTextField4.setBackground(resourceMap.getColor("jTextField4.background")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N
        jTextField4.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jTextField4InputMethodTextChanged(evt);
            }
        });
        jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField4KeyPressed(evt);
            }
        });

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jTextField5.setBackground(resourceMap.getColor("jTextField5.background")); // NOI18N
        jTextField5.setName("jTextField5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jTextField6.setBackground(resourceMap.getColor("jTextField6.background")); // NOI18N
        jTextField6.setName("jTextField6"); // NOI18N
        jTextField6.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jTextField6InputMethodTextChanged(evt);
            }
        });
        jTextField6.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField6KeyPressed(evt);
            }
        });

        jLabel8.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(Add, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Find, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Back, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Fwd, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(Edit, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel7))))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(Back, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Find, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Add, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Edit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Delete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Fwd))
                .addGap(11, 11, 11)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel7)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))))
                .addContainerGap(103, Short.MAX_VALUE))
        );

        jButton1.setVisible(false);

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hibernate_book.Hibernate_BookApp.class).getContext().getActionMap(Hibernate_BookView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        exitMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                exitMenuItemMouseClicked(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 358, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    private int maxLocs = 0;
    // tempLoc used for adding location set to db
    private static Set<Location> tempLoc = new HashSet<Location>();
    // finded humans contains in this set
    public static List<Human> humans = new ArrayList<Human>();
    private static int humanNumber = 0;
    private static Human tmpHum = new Human();
    private boolean isAdding = false;
    private static int spinnNumber = 0;
    private static String hql = "";
//    Session session = HibernateUtil.currentSession();

    private void updateFields() {
        if (humans.size() != 0) {
            Human h = humans.get(humanNumber);

            jTextField1.setText(h.getSecond_name());
            jTextField2.setText(h.getName());
            jTextField3.setText(h.getLast_name());
//            System.err.println(h.getLocations().toArray().length + "  " + h.getLocations().toArray().toString());
            if (h.getLocations().toArray().length != 0) {
                jSpinner1.setEnabled(true);
                Location l = (Location) h.getLocations().toArray()[spinnNumber];


                jTextField4.setText(l.getPhone());
                jTextField5.setText(l.getAdress());
                jTextField6.setText(l.getCaption());

                updateSpinnLabel(h.getLocations().size());
            } else {
                jSpinner1.setEnabled(false);
            }

        }
    }

    private void AddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddMouseClicked
//        Location l = new Location("grg");
//        l.setAdress("fff");
//        tempLoc.add(l);
//        System.out.println(tempLoc.size());
        jSpinner1.setEnabled(true);
        Back.setEnabled(false);
        Fwd.setEnabled(false);
        Delete.setEnabled(false);
        isAdding = true;
        Integer NULL = 0;
        jSpinner1.setValue((Object) NULL);
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
//        tempLoc = null;

        jButton1.setVisible(true);
        jButton2.setVisible(false);

    }//GEN-LAST:event_AddMouseClicked

    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
        Integer spin = (Integer) jSpinner1.getValue();
        spinnNumber = spin;
        Integer NULL = 0;

        if (spin < 0) {
            jSpinner1.setValue((Object) NULL);
        }


        boolean maxSpinChanged = false;
        if (isAdding) {
            updateSpinnLabel(maxLocs);
            if (maxLocs < spin) {
                maxLocs = spin;
                maxSpinChanged = true;
            }
            if ((spin < maxLocs) && (!maxSpinChanged)) {
                Location l = new Location();
                l = (Location) tempLoc.toArray()[spin];
                jTextField4.setText(l.getPhone());
                jTextField5.setText(l.getAdress());
                jTextField6.setText(l.getCaption());
            } else {
                Location l = new Location("none");
                l.setParent(tmpHum);
                l.setCaption(jTextField6.getText());
                l.setPhone(jTextField4.getText());
                l.setAdress(jTextField5.getText());
                if ((!l.getAdress().equals("")) || (!l.getCaption().equals("")) || (!l.getPhone().equals(""))) {
                    tempLoc.add(l);
                }
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField6.setText("");
            }
        } else if (humans.size() != 0) {
            if (spin >= humans.get(humanNumber).getLocations().toArray().length) {
                spin--;
                Integer max = spin;
                jSpinner1.setValue((Object) max);
            }
            Location l = (Location) humans.get(humanNumber).getLocations().toArray()[spin];

            jTextField4.setText(l.getPhone());
            jTextField5.setText(l.getAdress());
            jTextField6.setText(l.getCaption());

        }




//        System.out.println("######"+ jSpinner1.getValue());
    }//GEN-LAST:event_jSpinner1StateChanged
    private void updateSpinnLabel(int num) {
        String label = "из ";
        label += num;
        jLabel7.setText(label);

    }
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        maxLocs = 0;
        updateSpinnLabel(0);

        tmpHum = new Human(jTextField2.getText());
        tmpHum.setName(jTextField2.getText());
        tmpHum.setSecond_name(jTextField1.getText());
        tmpHum.setLast_name(jTextField3.getText());


        Location loc = new Location(jTextField6.getText());
        loc.setParent(tmpHum);
        loc.setCaption(jTextField6.getText());
        loc.setPhone(jTextField4.getText());
        loc.setAdress(jTextField5.getText());
        if ((!loc.getAdress().equals("")) || (!loc.getCaption().equals("")) || (!loc.getPhone().equals(""))) {
            tempLoc.add(loc);
        }

        tmpHum.setLocations(tempLoc);

        jSpinner1.setEnabled(false);
        Integer NULL = 0;
        jSpinner1.setValue((Object) NULL);
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");

        jButton1.setVisible(false);

        Session session = HibernateUtil.sessionFactory.openSession();

        Transaction transaction = session.beginTransaction();

        try {

            session.save(tmpHum);

            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
//             throw e;
            }
        } finally {
//            session.flush();
            tempLoc.clear();
            session.close();
        }
        tempLoc.clear();
        isAdding = false;



//        HibernateUtil.checkData("select * from human_table");
//        HibernateUtil.checkData("select * from location");

    }//GEN-LAST:event_jButton1MouseClicked

    private void jTextField4InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTextField4InputMethodTextChanged
    }//GEN-LAST:event_jTextField4InputMethodTextChanged

    private void jTextField4KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyPressed
//        int length = jTextField4.getText().length();
//        char last = jTextField4.getText().charAt(length - 1);
//
//        if ((last < '0' || last > '9') && (last != '+')) {
//            jTextField4.setText(jTextField4.getText().substring(0, length - 1));
//        }
    }//GEN-LAST:event_jTextField4KeyPressed

    private void jTextField6InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTextField6InputMethodTextChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField6InputMethodTextChanged

    private void jTextField6KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField6KeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField6KeyPressed
    private List<Human> findHumanList(String myhql) throws Exception {

        Session session = HibernateUtil.sessionFactory.openSession();
        Transaction transaction = null;
        List<Human> h = new ArrayList<Human>();


        //finding
        try {
            transaction = session.beginTransaction();

            h = session.createQuery(myhql).list();
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
//            throw e;
            }
        } finally {
            session.close();
        }
        return h;
    }

    private List<Location> findLocationList(String myhql) throws Exception {

        Session session = HibernateUtil.sessionFactory.openSession();
        Transaction transaction = null;
        List<Location> l = new ArrayList<Location>();


        //finding
        try {
            transaction = session.beginTransaction();

            l = session.createQuery(myhql).list();
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
                System.err.println("Locations finding error.");
//            throw e;
            }
        } finally {
            session.close();
        }
        return l;
    }
    private void FindMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FindMouseClicked

        humans.clear();
        humanNumber = 0;

        Integer NULL = 0;
        jSpinner1.setValue((Object) NULL);

        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jButton1.setVisible(false);
        isAdding = false;
        jButton2.setVisible(true);


    }//GEN-LAST:event_FindMouseClicked

    private void exitMenuItemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exitMenuItemMouseClicked
//        session.close();
    }//GEN-LAST:event_exitMenuItemMouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        hql = "from Human h where ";
        String and = "";
        boolean isnames = false;

        String s = jTextField1.getText();

        if (!s.equals("")) {
            hql = hql + and + "h.second_name = '" + s + "' ";
            and = " and ";
            isnames = true;
        }

        s = jTextField2.getText();
        if (!s.equals("")) {
            hql = hql + and + " h.name = '" + s + "' ";
            and = " and ";
            isnames = true;
        }

        s = jTextField3.getText();
        if (!s.equals("")) {
            hql = hql + and + " h.last_name = '" + s + "' ";
            and = " and ";
            isnames = true;
        }

        if (hql.trim().equals("from Human h where")) {
//            System.out.println("                        OK");

        } else {
//            System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
            try {
//                System.out.println(hql);
                humans = findHumanList(hql);

            } catch (Exception e) {
                System.err.println("Finding error");
            }
        }

//        if (!isnames) {
        hql = "from Location l where ";
        and = "";
        s = jTextField4.getText();
        if (!s.equals("")) {
            hql = hql + and + "l.phone = '" + s + "' ";
            and = " and ";

        }

        s = jTextField5.getText();
        if (!s.equals("")) {
            hql = hql + and + " l.adress = '" + s + "' ";
            and = " and ";

        }

        s = jTextField6.getText();
        if (!s.equals("")) {
            hql = hql + and + " l.caption = '" + s + "' ";
            and = " and ";

        }
//        }
        List<Location> locs = new ArrayList<Location>();
        Session session = HibernateUtil.sessionFactory.openSession();
        try {
            
            Transaction transaction = session.beginTransaction();


            locs = session.createQuery(hql).list();

            transaction.commit();
            
        } catch (Exception e) {
            System.err.println("Finding error");
        }

        if (locs.toArray().length != 0) {
            for (Location loc1 : locs) {
                if (!humans.contains(loc1.getParent())) {
                    Human h = loc1.getParent();
                    humans.add(h);
                    
//                    System.out.println(loc1.getParent().equals(null));
//                    System.err.print(h.getId() + "   " + h.getSecond_name());
                }
            }
        }

        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jSpinner1.setEnabled(true);
        updateFields();
        session.close();

//        jTextField4.setText("");
//        jTextField5.setText("");
//        jTextField6.setText("");
        Delete.setEnabled(true);
        Back.setEnabled(true);
        Fwd.setEnabled(true);
        jButton2.setVisible(false);

//        HibernateUtil.checkData("select * from human_table");
//        HibernateUtil.checkData("select * from location");

    }//GEN-LAST:event_jButton2MouseClicked

    private void BackMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BackMouseClicked
        if (humanNumber > 0) {
            humanNumber--;
            updateFields();
        }

    }//GEN-LAST:event_BackMouseClicked

    private void FwdMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FwdMouseClicked
        if (humanNumber < humans.size() - 1) {
            humanNumber++;
            updateFields();
        }

    }//GEN-LAST:event_FwdMouseClicked

    private void DeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DeleteMouseClicked
        if (humans.size() != 0) {
            Session session = HibernateUtil.sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(humans.get(humanNumber));
            Set locations = humans.get(humanNumber).getLocations();
            for (int i = 0; i < locations.size(); i++) {
                Location l = (Location) locations.toArray()[i];
                if (l != null) {
                    session.delete(l);
                }
            }

            transaction.commit();
            session.close();
            try {
//                System.out.println(hql);
                humans = findHumanList(hql);

            } catch (Exception e) {
                System.err.println("Error after deleting. Updating list of humans error.");
            }

            humanNumber = 0;
            updateFields();
        }
    }//GEN-LAST:event_DeleteMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add;
    private javax.swing.JButton Back;
    private javax.swing.JButton Delete;
    private javax.swing.JButton Edit;
    private javax.swing.JButton Find;
    private javax.swing.JButton Fwd;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
